package com.can.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.can.domain.ResponseResult;
import com.can.domain.entity.Link;
import com.can.domain.vo.PageVO;
import com.can.service.LinkService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/content/link")
public class LinkController {

    @Resource
    private LinkService linkService;

    @GetMapping("/list")
    public ResponseResult list(Integer pageNum, Integer pageSize, Link link) {
        Page<Link> result = new Page<>(pageNum, pageSize);
        linkService.page(result, new LambdaQueryWrapper<Link>()
                .like(StringUtils.hasText(link.getName()), Link::getName, link.getName())
                .eq(StringUtils.hasText(link.getStatus()), Link::getStatus, link.getStatus())
        );
        return ResponseResult.okResult(new PageVO(result.getRecords(), result.getTotal()));
    }

    @PostMapping
    public ResponseResult add(@RequestBody Link link) {
        linkService.save(link);
        return ResponseResult.okResult();
    }

    @GetMapping("/{id}")
    public ResponseResult get(@PathVariable Long id) {
        return ResponseResult.okResult(linkService.getById(id));
    }

    @PutMapping
    public ResponseResult get(@RequestBody  Link link) {
        linkService.updateById(link);
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{id}")
    public ResponseResult del(@PathVariable Long id) {
        linkService.removeById(id);
        return ResponseResult.okResult();
    }

}